{% extends 'base.html' %}

{% load filter_null_tag %}

{% block title %}数据分析{% endblock %}

{% block css %}{% endblock %}

{% block content %}
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-1">
                <input href="" type="button" value="新增case" class="form-control btn btn-success"
                       data-toggle="modal"
                       data-target="#singAddModal"
                       data-whatever="新增">
            </div>
            <div class="col-md-1">
                <input id="auditBtn" type="button" value="一键自动审核" class="form-control btn btn-warning">
            </div>
        </div>
        <br>
        <div class="row col-md-12">
            <div>
                <table class="table table-hover table-striped table-bordered table-dark">
                    <tr>
                        <th class="col-md-3">用例</th>
                        <th class="col-md-1">data结论</th>
                        <th class="col-md-1">sql结论</th>
                        <th class="col-md-1">一键审核结论</th>
                        <th class="col-md-1">人工复审结论</th>
                        <th class="col-md-3">备注</th>
                        <th class="col-md-2">操作</th>
                    </tr>

                    {% for obj in issues_object_list %}
                        <tr>
                            <td>{{ obj.cmd }}</td>
                            <td>{% null_to_empty obj.data_result %}</td>
                            <td>{% null_to_empty obj.sql_result %}</td>
                            <td>{% null_to_empty obj.sql_result %}</td>
                            <td>{% null_to_empty obj.manual_result %}</td>
                            <td>{% null_to_empty obj.comment %}</td>
                            <td>
                                <a href="" class="btn btn-warning btn-xs" type="button"
                                   data-toggle="modal"
                                   data-target="#checkModal"
                                   data-whatever="人工审核"
                                   data-fid="{{ obj.id }}">
                                    <i class="fa fa-pencil-square-o" aria-hidden="true">人工复审</i>
                                </a>
                                <a href="" class="btn btn-success btn-xs" type="button">
                                    <i class="fa fa-pencil-square-o" aria-hidden="true">增加记录</i>
                                </a>
                                <a href="" class="btn btn-info btn-xs" type="button"
                                   data-toggle="modal"
                                   data-target="#myModal"
                                   data-whatever="对比详情"
                                   data-fid="{{ obj.id }}">
                                    <i class="fa fa-pencil-square-o" aria-hidden="true"> 详情</i>
                                </a>
                                <a href="" class="btn btn-danger btn-xs" type="button"
                                   data-toggle="modal"
                                   data-target="#deleteModal"
                                   data-whatever="删除case确认"
                                   data-fid="{{ obj.id }}">
                                    <i class="fa fa-trash" aria-hidden="true"> 删除</i>
                                </a>
                            </td>
                        </tr>
                    {% endfor %}
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-12">
        <nav aria-label="Page navigation" style="text-align: right">
            <ul class="pagination" style="margin-top: 0">
                {{ page_html | safe }}
            </ul>
        </nav>
    </div>
    <!-- detail Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
                <div class="modal-body">
                    <form action="" id="form">
                        {% csrf_token %}
                        <table class="table table-bordered table-hover" id="mytable">
                            <tbody>
                            </tbody>
                        </table>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关 闭</button>
                    <button type="button" class="btn btn-primary" id="btnFormsubmit">确 认</button>
                </div>
            </div>
        </div>
    </div>

    <!-- add Modal -->
    <div class="modal fade" id="singAddModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
                <div class="modal-body">
                    <form action="" id="form">
                        {% csrf_token %}
                        <div class="form-group">
                            <label for="caseName">*用例名称</label>
                            <input type="text" class="form-control" id="caseName">
                            <span class="error-msg" style="color: red"></span>
                        </div>
                        <div class="form-group">
                            <label for="comment">备注</label>
                            <input type="text" class="form-control" id="comment">
                            <span class="error-msg" style="color: red"></span>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关 闭</button>
                    <button type="button" class="btn btn-primary" id="confirmBtn">确 认</button>
                </div>
            </div>
        </div>
    </div>

    <!-- check Modal -->
    <div class="modal fade" id="checkModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">人工审核</h4>
                </div>
                <div class="modal-body">
                    <form action="" id="form">
                        {% csrf_token %}
                        <div class="form-group">
                            <label for="peoCheckResult">审核结论(Pass/Fail)</label>
                            <input type="text" class="form-control" id="peoCheckResult">
                            <span class="error-msg" style="color: red"></span>
                        </div>
                        <div class="form-group">
                            <label for="peopelComment">备注</label>
                            <input type="text" class="form-control" id="peopelComment">
                            <span class="error-msg" style="color: red"></span>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关 闭</button>
                    <button type="button" class="btn btn-primary" id="checkBtn">确 认</button>
                </div>
            </div>
        </div>
    </div>

    <!-- delete moddal -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">删除确认</h4>
                </div>
                <div class="modal-body">
                    <span class="delete-des">确定要删除此case？</span>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关 闭</button>
                    <button type="button" class="btn btn-primary" id="btnDeleteSubmit">确 认</button>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script>
        $(function () {
            initDiffModal();
            addSingleCase();
            caseSubmit();
            delModalCase();
            delCaseSubmit();
            auditAllCase();    // 一键审核所有case
            peopleCheckModal();      // 人工审核 模态框
            peopleCheckbtn();      // 人工审核 提交
        })

        // 详情模态框：生成表格
        function initDiffModal() {
            // 模态框显示出来后，触发function (event)函数
            $('#myModal').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget) // Button that triggered the modal
                var recipient = button.data('whatever') // Extract info from data-* attributes
                var modal = $(this)
                modal.find('.modal-title').text(recipient)
                {#modal.find('.modal-body input').val(recipient)#}

                modal.find('.error-msg').empty().parent().removeClass('has-error')    // 清空报错信息
                {#$('#form')[0].reset()   //转换成document对象后，使用重置表单功能#}
                $('#mytable tbody tr').remove()  // 重置表格

                var basedata_id = button.data('fid')
                $.ajax({
                    url: "{% url 'analy:get_comparedata' %}",
                    type: "post",
                    data: {'csrfmiddlewaretoken': '{{csrf_token}}', 'basedata_id': basedata_id,},
                    dataType: "JSON",
                    success: function (response) {
                        var res = JSON.parse(response.compare_data)
                        console.log(res)
                        var thead = $("<tr><th>version</th><th>chart</th><th>tts</th><th>sql</th><th>data</th></tr>")
                        $("#mytable tbody").append(thead)
                        for (let i = 0; i < res.length; i++) {
                            var newRow = $("<tr></tr>")
                            newRow.append("<td>" + res[i].fields.version + "</td>" + "<td>" + res[i].fields.recv_chart + "</td>" + "<td>" + res[i].fields.recv_tts + "</td>" + "<td>" + res[i].fields.recv_sql + "</td>" + "<td>" + res[i].fields.recv_data + "</td>")
                            $("#mytable tbody").append(newRow);
                        }
                    }
                })
            })
        }

        // 新增模态框：初始化
        function addSingleCase() {
            $('#singAddModal').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget)
                var recipient = button.data('whatever')
                var modal = $(this)
                modal.find('.modal-title').text(recipient)
                $('.error-msg').empty();
            })
        }

        // 新增模态框：点确认
        function caseSubmit() {
            $("#confirmBtn").click(function () {
                var cmd = $("#caseName").val()
                var comment = $("#comment").val()
                $.ajax({
                    url: "{% url 'analy:add_sing_case' %}",
                    type: "post",
                    data: {"cmd": cmd, "comment": comment, 'csrfmiddlewaretoken': '{{ csrf_token }}',},
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            $("#singAddModal").modal('hide')
                            location.reload()
                        } else {
                            var error = res.message
                            $("#caseName~span").text(error)
                        }
                    }
                })
            })
        }

        // 删除模态框：获取要删除的id
        function delModalCase() {
            $('#deleteModal').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget) // Button that triggered the modal
                var recipient = button.data('whatever') // Extract info from data-* attributes
                var modal = $(this)
                modal.find('.modal-title').text(recipient)
                var fid = button.data('fid')
                $("#btnDeleteSubmit").val(fid)
            })
        }

        // 删除case：点确认
        function delCaseSubmit() {
            $("#btnDeleteSubmit").click(function () {
                var delCaseId = $("#btnDeleteSubmit").val()
                console.log(delCaseId)
                $.ajax({
                    url: "{% url 'analy:del_sing_case' %}",
                    type: "post",
                    data: {"delCaseId": delCaseId, 'csrfmiddlewaretoken': '{{ csrf_token }}',},
                    datatype: "JSON",
                    success: function (res) {
                        $("#deleteModal").modal('hide')
                        location.reload()
                    }
                })
            })
        }

        // case一键自动审核
        function auditAllCase() {
            $("#auditBtn").click(function () {
                $.ajax({
                    url: "{% url 'analy:audit_cases' %}",
                    type: "post",
                    data: {"csrfmiddlewaretoken": '{{ csrf_token }}',},
                    dataType: "JSON",
                    success: function (res) {
                        if (res.status) {
                            console.log(typeof res.status)
                            console.log(res.status)
                            location.reload()
                        }
                    }
                })
            })
        }

        //人工审核模态框：触发事件
        function peopleCheckModal() {
            // 模态框显示出来后，触发function (event)函数
            $('#checkModal').on('show.bs.modal', function (event) {
                var button = $(event.relatedTarget) // Button that triggered the modal
                var recipient = button.data('whatever') // Extract info from data-* attributes
                var modal = $(this)
                modal.find('.modal-title').text(recipient)
                var basedata_id = button.data('fid')
                modal.find('#checkBtn').val(basedata_id)
            })
        }

        // 人工审核：点确认提交
        function peopleCheckbtn() {
            $("#checkBtn").click(function () {
                var basedata_id = $("#checkBtn").val()
                var peoCheckResult = $("#peoCheckResult").val()
                var peopelComment = $("#peopelComment").val()
                console.log(basedata_id)
                console.log(peoCheckResult)
                $.ajax({
                    url: "{% url 'analy:people_check' %}",
                    type: "post",
                    data: {
                        'csrfmiddlewaretoken': '{{csrf_token}}',
                        'basedata_id': basedata_id,
                        "peoCheckResult": peoCheckResult,
                        "peopelComment": peopelComment
                    },
                    dataType: "JSON",
                    success: function (res) {
                        console.log(res)
                        $("#checkModal").modal('hide')
                        location.reload()
                    }
                })
            })
        }
    </script>
{% endblock %}
